package com.lh.servlet;

import org.springframework.lang.NonNull;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet请求实现方法一
 * 注解@WebServlet：申明此类为Servlet类
 * 配合@ServletComponentScan使用
 * @Author LuoHang
 * @Date 2022-05-10 17:51
 **/
@WebServlet(urlPatterns = "/myServlet01")
public class MyServlet01 extends HttpServlet {
    @Override
    protected void doGet(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String username = req.getParameter("username");
        PrintWriter writer = resp.getWriter();
        writer.write("Hello!" + username);
        writer.flush();
        writer.close();
    }
}
